<template>
  <div class="sub-category" v-if="subCate" ref="target">
    <div class="container">
      <!-- 面包屑 -->
      <SubBread />
      <!-- 筛选区 -->
      <SubFilter />
      <!-- 结果区域 -->
      <div class="goods-list">
        <!-- 排序 -->
        <SubSort />
        <!-- 商品列表 -->
        <ul class="list">
          <li v-for="i in list" :key="i.id">
            <GoodsItem :goods="i"></GoodsItem>
          </li>
        </ul>
        <!-- 分页 -->
        <XtxInfiniteLoading
          v-model="loading"
          :finished="finished"
          @load="getList"
          finished-text="暂时没有更多商品了"
          loading-text="正在玩命加载中..."
        >
        </XtxInfiniteLoading>
      </div>
    </div>
  </div>
</template>

<script>
import SubSort from './components/sub-sort.vue'
import GoodsItem from './components/goods-item.vue'
import SubFilter from './components/sub-filter'
import { findCategoryGoods, findSubCategory } from '@/api/category'
import { useRoute } from 'vue-router'
import SubBread from './components/sub-bread'
import { provide, watch, ref, reactive } from 'vue'

export default {
  name: 'SubCategory',
  components: { SubBread, SubFilter, SubSort, GoodsItem },
  setup () {
    // 分页功能
    const finished = ref(false)
    const loading = ref(false)
    const page = ref(1)

    const route = useRoute()
    // console.log(route.params.id)
    const subCate = ref(null)

    // 清空跳转路由时，排序列表的数据
    const defaultParamsFn = () => {
      return {
        categoryId: route.params.id,
        sortField: null,
        sortMethod: null,
        inventory: false,
        onlyDiscount: false,
        attrs: [],
        brandId: null,
        pageSize: 20
      }
    }

    watch(
      () => route.params.id,
      async (id) => {
        if (route.path !== `/category/sub/${id}`) return
        // console.log(id)
        const { result } = await findSubCategory(id)
        // console.log('result', result)
        subCate.value = result

        const obj = defaultParamsFn()

        for (const key in obj) {
          sortParams[key] = obj[key]
        }
      },
      { immediate: true }
    )

    // 通过 provide 提供数据给所有的后代组件
    provide('subCate', subCate)

    const sortParams = reactive(defaultParamsFn())

    provide('sortParams', sortParams)

    // 获取展示商品
    const list = ref([])
    const getList = async () => {
      loading.value = true
      const { result } = await findCategoryGoods({ ...sortParams, page: page.value })
      // console.log('展示商品', result)
      loading.value = false
      list.value.push(...result.items)
      // console.log('list', list)
      page.value++
      if (result.items.length < sortParams.pageSize) {
        finished.value = true
      }
    }

    watch(sortParams, () => {
      page.value = 1
      list.value = []
      finished.value = false
      getList()
    }, { immediate: true })
    // console.log(list)

    return { subCate, list, getList, loading, finished, page }
  }
}
</script>

<style scoped lang="less">
.goods-list {
  background: #fff;
  padding: 0 25px;
  margin-top: 25px;
  ul {
    display: flex;
    flex-wrap: wrap;
    padding: 0 5px;
    li {
      margin-right: 20px;
      margin-bottom: 20px;
      &:nth-child(5n) {
        margin-right: 0;
      }
    }
  }
}
</style>
